
import util from "../../common/js/util.js";
var app = getApp();
let base = {

  CC_LISTS: [],
  data1: [],
  data2: [],
  USER_NAME: ""
}
Component({
  mixins: [],
  data: {
    COMMENT_LIST: util.copy(base),
    typeName: '',
    choose_people: false,
    data: {}
  },
  props: {
    approvalApi: Object,
    choose_people: false,//开启 选择审批人员 模式
  },
  didMount() {
    this.setData({
      COMMENT_LIST: util.copy(base),
      choose_people: this.props.choose_people
    })

  },
  didUpdate() {
    //console.log(this.props.approvalApi);
  },
  didUnmount() {

  },
  methods: {
    openFn(approvalApi) {
      this.props.approvalApi = approvalApi;

      this.initData();
    },
    getDaishenByItem(node) {
      /**
       * 重要数据
       * node.APPROVAL_DAISHEN_LIST
       * node.USER_NAME
       * node.detailTypeName : examination  compoundG2E  compoundE2G gradually
       */
      try {
        node.APPROVAL_SHOWS = JSON.parse(node.APPROVAL_SHOW);
        let APPROVAL_DAISHEN_LIST = []
        for (let i in node.APPROVAL_SHOWS) {
          // if (node.APPROVAL_SHOWS[i].Approval == "待审批") {
          //   APPROVAL_LIST += `,${node.APPROVAL_SHOWS[i].UserName}`;
          // }
          if (node.APPROVAL_SHOWS[i].Approval != "提起审批") {
            node.APPROVAL_SHOWS[i].pass = false;
            if ((node.APPROVAL_SHOWS[i].Approval.indexOf("通过") != -1) ||
              (node.APPROVAL_SHOWS[i].Approval.indexOf("同意") != -1)
            ) {
              node.APPROVAL_SHOWS[i].pass = true;
            }
            node.APPROVAL_SHOWS[i].detailTypeName = util.getDetailApprovalTypeName(node.TYPE_NAME);
            APPROVAL_DAISHEN_LIST.push(node.APPROVAL_SHOWS[i]);
          }
        }
        node.APPROVAL_DAISHEN_LIST = APPROVAL_DAISHEN_LIST;
        node.detailTypeName = util.getDetailApprovalTypeName(node.TYPE_NAME);
      } catch (e) {
        console.log(e);
      }

      return node;
    },
    initData() {
      this.setData({
        typeName: this.props.approvalApi.typeName,
        choose_people: this.props.choose_people,
        approvalApi:this.props.approvalApi
      })

      //this.props.approvalApi. view_data
      if(util.isNotEmpty(this.props.approvalApi.PID)){
        app.$post(this.props.approvalApi.apiApprovalOpen.url, this.props.approvalApi.apiApprovalOpen.data).then(res => {

          if (res.is == 1) {
            app.$post(this.props.approvalApi.apiApprovalOpen.url, this.props.approvalApi.apiApprovalOpen.data).then(res => {
  
              if (res.is == 1) {
      
                let data1 = {}, data2 = null, data = {};
                
                if (util.isNotEmpty(res.data.data1)||util.isNotEmpty(res.data.data2)) {
                  data = res.data.data1;
                  if (util.isNotEmpty(res.data.data1)) {
                    data1 = this.getDaishenByItem(res.data.data1);
                  }
                  if (util.isNotEmpty(res.data.data2)) {
                    data2 = this.getDaishenByItem(res.data.data2);
                  }
                } else {
                  data = res.data;
                  if(util.isNotEmpty(res.data)){
                    data1 = this.getDaishenByItem(res.data);
                  }
                }
                this.setData({
                  data
                })
                
                let COMMENT_LIST = util.copy(base);
                COMMENT_LIST.data1 = data1.APPROVAL_DAISHEN_LIST;
                if (util.isNotEmpty(data2)) {
                  COMMENT_LIST.data2 = data2.APPROVAL_DAISHEN_LIST;
                }
      
                COMMENT_LIST.USER_CN_NAME = data1.USER_CN_NAME;
      
                let CC_LISTS = [];
      
                try {
                  let str_arr = data1.CC_LIST.split(",");
                  for (let i in str_arr) {
                    let name = app.prototype.$USERNAME_MAP.get(str_arr[i]);
                    CC_LISTS.push({
                      via: name.slice(name.length - 2, name.length),
                      name: name,
                      UserName: str_arr[i],
                      Approval: "抄送人"
                    })
                  }
                } catch (e) { }
                COMMENT_LIST.CC_LISTS = CC_LISTS;
      
                //父级页面 控制不显示 1
                if (util.isNotEmpty(this.props.approvalApi.show_people_list) && this.props.approvalApi.show_people_list == "no_show") {
                  COMMENT_LIST = util.copy(base);
                }
                this.setData({
                  COMMENT_LIST: COMMENT_LIST
                })
      
              } else {
                this.setData({
                  COMMENT_LIST: util.copy(base)
                })
              }
              dd.hideLoading();
            }).catch(e => {
      
            });
          }
        });
  
      }else{
        let COMMENT_LIST = util.copy(base);
        if(util.isNotEmpty(this.props.approvalApi.view_data.CC_LIST)){
          COMMENT_LIST.CC_LISTS = this.idsParseToApprovalRuleList(this.props.approvalApi.view_data.CC_LIST,this.props.approvalApi.detailTypeName);
        }
        if(util.isNotEmpty(this.props.approvalApi.view_data.APPROVAL_LIST)){
          // if(this.props.approvalApi.detailTypeName=="examination"){

          // }else{

          // }
          COMMENT_LIST.data1 = this.idsParseToApprovalRuleList(this.props.approvalApi.view_data.APPROVAL_LIST,this.props.approvalApi.detailTypeName);

        }
        this.setData({
          COMMENT_LIST
        })
      }

    },
    idsParseToApprovalRuleList(str = "",TYPE_NAME) {
      let list = str.split(",");
      let RECEIVERS = [];
      for (let index = 0; index < list.length; index++) {
        let user_en_name = list[index];
        let user_cn_name = app.prototype.$USERNAME_MAP.get(user_en_name);
        if (util.isNotEmpty(user_cn_name)) {
          let json = {
            UserName: user_en_name,
            UserCnName: user_cn_name,
            name: user_cn_name,
            detailTypeName:TYPE_NAME,
            no_delete:false
          }
          RECEIVERS.push(json);
        };
      }
      return RECEIVERS;
    },
    onIndexPass(e) {
      
      this.setData({
        COMMENT_LIST: e
      })
    },
    choose_people_click(e) {
      // let detailTypeName=this.props.approvalApi.detailTypeName;
      //  let data=this.data.data;
      //  debugger
      let approvalApi = this.props.approvalApi;
      // approvalApi.view_data=data;
      this.approver_people_choose.openFn(approvalApi);
    },
    approver_people_choose(ref) {
      this.approver_people_choose = ref
    },
    //发送评论
    commentSend(msg) {

      let pid = this.props.approvalApi.PID;
      let CommentType = this.props.approvalApi.COMMENT_TYPE_NAME;
      let list = {
        TYPE_NAME: CommentType,
        COMMENTS: encodeURI(msg),
        PID: pid + CommentType
      }
      let api = "/YhERP/articleread/json/save/URLEncode";
      app.$post(api, list).then(res => {
        if (res.is == 1) {

        }
      })
    },
    
  },
});
